package com.a4studio.android.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;
import android.util.Xml;

public class YahooNewsCrawler {

	private final static String TAG=YahooNewsCrawler.class.getSimpleName();
	private String language = "en";
	private String keyword;
	private String site;
	private int    results;
	private String uri= "";    
	
	private final static String NEWS_SERVICE = "http://search.yahooapis.com/NewsSearchService/V1/newsSearch?";
	private final static String APP_ID = "iEjUXGrV34HmLcV9m1gm1OBUWUve.fUcYv553gw.MUHn5b8BA_8W8Fe4AxuOLMKKLLU-";
	
	private List<NewsItem> newsResults = new ArrayList<NewsItem>();
	
	public YahooNewsCrawler(String keyword,int results)
	{
		this.keyword = keyword;
		this.results = results;				
	}
	public void search()
	{
		XmlPullParser xmlPull = Xml.newPullParser();
		
		URL urlObj;
	
			try {
				keyword = URLEncoder.encode(keyword,"UTF-8");
				uri = NEWS_SERVICE+"appid="+APP_ID+"&"+
			      "query="+keyword+"&"+"" +
			      "results="+results+"&"+
			      "language=en";
				
				Log.d(TAG,uri);
				urlObj = new URL(uri);
				URLConnection conn = urlObj.openConnection();
				
				xmlPull.setInput(conn.getInputStream(),"UTF-8");
				
				int eventCode = xmlPull.getEventType();
				
				NewsItem item = null;
				String name ;
				
				while(eventCode != XmlPullParser.END_DOCUMENT)
				{
				switch (eventCode) {
				case XmlPullParser.START_DOCUMENT:
					break;
				case XmlPullParser.START_TAG:
					name = xmlPull.getName();
					if (name.equalsIgnoreCase(NewsItem.RESULT)) {
						item = new NewsItem();
					} else if (item != null) {
						String text = xmlPull.nextText();
						if (name.equalsIgnoreCase(NewsItem.TITLE)) {
							item.setTitle(text);
						} else if (name.equalsIgnoreCase(NewsItem.SUMMARY)) {
							item.setSummary(text);
						} else if (name.equalsIgnoreCase(NewsItem.URL)) {
							item.setUrl(text);
						} else if (name.equalsIgnoreCase(NewsItem.CLICKURL)) {
							item.setClickUrl(text);
						} else if (name.equalsIgnoreCase(NewsItem.NEWSSOURCE)) {
							item.setNewsSource(text);
						} else if (name.equalsIgnoreCase(NewsItem.NEWSSOURCEURL)) {
							item.setNewsSrouceUrl(text);
						} else if (name.equalsIgnoreCase(NewsItem.LANGUAGE)) {
							item.setLanguage(text);
						} else if (name.equalsIgnoreCase(NewsItem.PUBLISHDATE)) {
							item.setPublishDate(Long.parseLong(text));
						} else if (name.equalsIgnoreCase(NewsItem.MODIFICATIONDATE)) {
							item.setModifDate(Long.parseLong(text));
						}
					}
					break;
				case XmlPullParser.END_TAG:
					if(item != null && xmlPull.getName().equalsIgnoreCase(NewsItem.RESULT))
					{
						newsResults.add(item);
						item = null;
					}
					break;
				default:
					break;
						
				}
				
				eventCode = xmlPull.next();
			}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				Log.e(TAG, "search", e);
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				Log.e(TAG, "search", e);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				Log.e(TAG, "search", e);
			} catch (XmlPullParserException e) {
				// TODO Auto-generated catch block
				Log.e(TAG, "search", e);
			}


	}
	

	/**
	 * @return the newsResults
	 */
	public List<NewsItem> getNewsResults() {
		return newsResults;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		YahooNewsCrawler crawler = new YahooNewsCrawler("nokia siemens network",1);
		crawler.search();
		
		List<NewsItem> items = crawler.getNewsResults();
		
		for(NewsItem item : items)
		{
			System.out.println("->"+item);
		}
	}

}
